﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Text;
using Nb.Common;

namespace Nb.Licenses.Libs.Hardwares
{
    public class MachineInfo
    {
        public MachineInfo(IEnumerable<string> cpus, IEnumerable<string> disks, IEnumerable<string> macs)
        {
            CpuSerialNumbers = new List<HardwareSerialNumber>();
            HardDiskSerialNumbers = new List<HardwareSerialNumber>();
            Macs = new List<HardwareSerialNumber>();

            var cpuList = cpus as IList<string> ?? cpus.ToList().OrderBy(x => x).ToList();
            for (int i = 0; i < cpuList.Count; i++)
            {
                CpuSerialNumbers.Add(new HardwareSerialNumber() { Number = cpuList[i], Order = i });
            }
            
            var diskList = disks as IList<string> ?? disks.ToList().OrderBy(x => x).ToList();
            for (int i = 0; i < diskList.Count; i++)
            {
                HardDiskSerialNumbers.Add(new HardwareSerialNumber() { Number = diskList[i], Order = i });
            }

            var macList = macs as IList<string> ?? macs.ToList().OrderBy(x => x).ToList();
            for (int i = 0; i < macList.Count; i++)
            {
                Macs.Add(new HardwareSerialNumber() { Number = macList[i], Order = i });
            }
        }
        
        /// <summary>
        /// Cpu Serial Number
        /// </summary>
        public IList<HardwareSerialNumber> CpuSerialNumbers { get; set; }
        /// <summary>
        /// HardDisk Serial Number
        /// </summary>
        public IList<HardwareSerialNumber> HardDiskSerialNumbers { get; set; }
        /// <summary>
        ///  Network Card Serial Number
        /// </summary>
        public IList<HardwareSerialNumber> Macs { get; set; }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("----Machine Info ({0})----", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            sb.AppendFormat("CPU[{0}]: {1}{2}", CpuSerialNumbers.Count, MyStringHelper.MakeSplitString(CpuSerialNumbers.OrderBy(x => x.Order).Select(x => x.Number).ToArray()), Environment.NewLine);
            sb.AppendFormat("DISK[{0}]: {1}{2}", HardDiskSerialNumbers.Count, MyStringHelper.MakeSplitString(HardDiskSerialNumbers.OrderBy(x => x.Order).Select(x => x.Number).ToArray()), Environment.NewLine);
            sb.AppendFormat("MAC[{0}] {1}{2}", Macs.Count, MyStringHelper.MakeSplitString(Macs.OrderBy(x => x.Order).Select(x => x.Number).ToArray()), Environment.NewLine);
            sb.AppendLine("--------------------");
            return sb.ToString();
        }
    }
    public class HardwareSerialNumber
    {
        public int Order { get; set; }
        public string Number { get; set; }
    }
}